import { JXBtlAns_PressModel, JxBtlCollisionType, JXBtlPs, JXBtlStepType } from './JXBattleDefine';
import JXBtlMgr from './JXBattleManager';
import { JXBattleEntity, JXBattleTeamEntiry } from "./JXBattleRole";
import { JXBattleSkill } from './JXBattleSkill';

export class JXBattleStep {
	public type: JXBtlStepType = JXBtlStepType.Null;
	public entity: JXBattleEntity = null;
	public nRound: number;
	constructor(entity: JXBattleEntity, nRound: number) {
		this.entity = entity;
		this.nRound = nRound;
	}

	/** 执行 */
	public exec() {
		console.log("11")
	}

	/** 执行完成 */
	public completed() {

	}

	/** 追击检测处理 */
	protected dealWithPursuit(nCombo: number, hurtInfo: JXAnsHurtInfo): boolean {
		if (nCombo == JXBtlPs.Invalid || this.entity.isDie) return false;
		if (!hurtInfo) return;
		let skill = (this.entity as JXBattleTeamEntiry).onAfterComAttack(this.nRound);
		if (skill) {
			// 触发追击技能,将追击动作插入队列
			hurtInfo.hasPursuit = true;
			JXBtlMgr.ins.addStep(new JXBattlePursuitStep(this.entity, this.nRound, skill, nCombo));
			return true;
		}
		return false;
	}

	/** 反击检测处理 */
	protected dealWithBeatback(): boolean {
		let lastHurters = this.entity.popHurters();
		let isBeatback = false;
		for (let i = 0; i < lastHurters.length; i++) {
			let entity = lastHurters[i];
			if (entity.isDie) continue;
			let hurter = entity as JXBattleTeamEntiry;
			let skill = hurter.onBeatbackSkill(this.nRound);
			if (skill) {
				JXBtlMgr.ins.addStep(new JXBattleHurtStep(hurter, this.nRound, skill, this.entity));
				isBeatback = true;
			}
		}
		return isBeatback;
	}

	/** 连击检测处理 */
	protected dealWithCombo(nCombo: number): boolean {
		if (this.entity.collision != JxBtlCollisionType.Normal) return false;
		// 没有普攻过，也就没有连击
		if (nCombo == JXBtlPs.Invalid) return false;
		// 连击检测
		let isCombo = !this.entity.isDie && (this.entity as JXBattleTeamEntiry).onCombo();
		if (isCombo) {
			JXBtlMgr.ins.addStep(new JXBattleComboStep(this.entity, this.nRound, nCombo));
			return true;
		}
		return false;
	}

}
/** buff处理行动逻辑 */
export class JXBattleBuffTake extends JXBattleStep {
	public type = JXBtlStepType.Buff;

	public exec() {
		this.entity.onControlRoundBuff();
		if (this.entity.isDie) return;
		this.entity.onLockHp();
		this.entity.onStartRoundBuff(this.nRound);
	}

	public completed() {
		if (this.entity.isDie) return;
		JXBtlMgr.ins.addStep(new JXBattleRdMain(this.entity, this.nRound));
	}

}

/** 主技能行动逻辑 */
export class JXBattleRdMain extends JXBattleStep {
	public type = JXBtlStepType.Main;
	/** 表示当前普攻次数，为负数表示没有普攻 */
	protected _atkCombo: number = JXBtlPs.Invalid;
	/** 如果存在普通攻击，用来引用当前普攻的伤害信息结构 */
	protected _comAtkHurtInfo: JXAnsHurtInfo;

	public exec() {
		//如果存在主动技能释放，则结束本次行动
		let isInitialSkill = this.entity.onNormalSkillAction(this.nRound);
		if (isInitialSkill) return;
		// 准备回合只进行技能检测
		if (this.nRound == JXBtlPs.FirstRd) return;
		// 非正常成员没有普工设定 
		if (this.entity.collision != JxBtlCollisionType.Normal) return;
		// 非队伍成员没有普工设定
		if (this.entity instanceof JXBattleTeamEntiry) {
			this._comAtkHurtInfo = this.entity.onComAttack(0);
			if (this._comAtkHurtInfo) {
				this._atkCombo++;
			}
		}
	}

	public completed() {
		// 准备回合只进行技能检测
		if (this.nRound == JXBtlPs.FirstRd) return;
		if (this.dealWithPursuit(this._atkCombo, this._comAtkHurtInfo)) return;
		this.dealWithBeatback();
		this.dealWithCombo(this._atkCombo);
	}
}

/** 反击(受击方行动)步骤 */
export class JXBattleHurtStep extends JXBattleStep {
	public type = JXBtlStepType.Beatback;
	/** 准备反击的技能 */
	protected _skill: JXBattleSkill;
	/** 攻击方 */
	protected _atker: JXBattleEntity;

	constructor(entity: JXBattleTeamEntiry, nRound: number, skill: JXBattleSkill, atker: JXBattleEntity) {
		super(entity, nRound);
		this._skill = skill;
		this._atker = atker;
	}

	/** 释放被动技能 */
	public exec() {
		this._skill.press(JXBtlAns_PressModel.Beatback);
	}

	public completed() {
		if (this._atker.isDie) return;
		// 清理掉缓存的受击对象
		this.entity.popHurters();
	}
}

/** 追击步骤 */
export class JXBattlePursuitStep extends JXBattleStep {
	public type = JXBtlStepType.Pursuit;
	protected skill: JXBattleSkill;
	protected _atkCombo: number;

	constructor(entity: JXBattleEntity, nRound: number, skill: JXBattleSkill, atkCombo: number) {
		super(entity, nRound);
		this.skill = skill;
		this._atkCombo = atkCombo;
	}

	public exec() {
		this.skill.press(JXBtlAns_PressModel.Pursuit);
	}

	public completed() {
		this.dealWithBeatback();
		this.dealWithCombo(this._atkCombo);
	}

}

export class JXBattleComboStep extends JXBattleStep {
	/** 普攻连击次数 */
	protected _atkCombo: number;
	/** 如果存在普通攻击，用来引用当前普攻的伤害信息结构 */
	protected _comAtkHurtInfo: JXAnsHurtInfo;
	constructor(entity: JXBattleEntity, nRound: number, atkCombo: number) {
		super(entity, nRound);
		this._atkCombo = atkCombo;
	}
	public exec() {
		if (this.entity.isDie) return false;
		let atker = this.entity as JXBattleTeamEntiry;
		this._comAtkHurtInfo = atker.onComAttack(this._atkCombo + 1);
		if (this._comAtkHurtInfo) {
			this._atkCombo++;
		}
	}

	public completed() {
		if (this.dealWithPursuit(this._atkCombo, this._comAtkHurtInfo)) return;
		this.dealWithBeatback();
		this.dealWithCombo(this._atkCombo);
	}
}
